<template>
  <div class="Order">
    <h2>用户信息</h2>
    <van-contact-card type="edit" :name="currentContact.name" :tel="currentContact.tel" @click="onEdit" />

    <h2>当前剩余桌台</h2>
    <div class="table">
      <div class="tab" ref="tab" v-for="(item, index) in tableData" :key="item.tid" @click="atChecked(index)">{{ item.tid }}号桌台</div>
    </div>
    <van-submit-bar :price="$route.params.totalPrice * 100 || 0" button-text="立即支付" @submit="onSubmit" />
  </div>
</template>

<script>
import { Dialog } from "vant";
import { Toast } from "vant";

export default {
  name: "Order",
  data() {
    return {
      tableData: [],
      currentContact: {
        name: JSON.parse(sessionStorage.getItem("userEat")).contactName,
        tel: JSON.parse(sessionStorage.getItem("userEat")).phone,
      },
    };
  },
  mounted() {
    this.$http.get("/order/table").then((res) => {
      this.tableData = res.data;
    });
  },
  methods: {
    atChecked(index) {
      for (let i = 0; i < this.tableData.length; i++) {
        this.$refs.tab[i].style.backgroundColor = "white";
      }
      this.$refs.tab[index].style.backgroundColor = "#ccc";
      console.log("桌台id", this.tableData[index].tid);
    },
    onSubmit() {
      console.log("ok");
      Dialog.confirm({
        message: `<img src='https://xp-bird.gitee.io/food-image/pay.jpg'  style='width:100%'/>`,
        confirmButtonText: "确认支付",
        cancelButtonText: "取消支付",
      })
        .then(() => {
          // on confirm
          Toast.success("支付成功");
        })
        .catch(() => {
          // on cancel
          Toast.fail("支付失败");
        });
    },
    // 编辑
    onEdit() {
      console.log("11");
    },
  },
};
</script>

<style lang="less" scoped>
.Order {
  .table {
    width: 100%;
    display: flex;
    flex-wrap: wrap;
    // justify-content: space-between;
    .tab {
      width: 24%;
      height: 2rem;
      border-radius: 0.1rem;
      outline: 0.01rem solid #cfcfcf;
      text-align: center;
      line-height: 2rem;
      margin-bottom: 0.1rem;
      margin-left: 0.05rem;
    }
  }
}
</style>
